﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LogitechLCDWrapper;

namespace LCDNetworkMonitor
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MonoLCDScreen screen;
        List<System.Diagnostics.PerformanceCounter> bytesReceived, bytesSent;
        System.Timers.Timer refreshTimer;
        int counterIdx, displayTypeIdx = 1;

        public MainWindow()
        {
            InitializeComponent();
            screen = new MonoLCDScreen("Network Monitor");
            screen.ButtonClicked += screen_ButtonClicked;
            refreshTimer = new System.Timers.Timer(1000);
            refreshTimer.Elapsed += refreshTimer_Elapsed;
            counterIdx = 0;
        }

        void refreshTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            string interfaceName = bytesReceived[counterIdx].InstanceName;
            float rValue = bytesReceived[counterIdx].NextValue();
            float sValue = bytesSent[counterIdx].NextValue();
            switch (displayTypeIdx)
            {
                case 0:
                    ShowInformationByText(interfaceName, rValue, sValue);
                    break;
                case 1:
                    ShowInformationByBar(interfaceName, rValue, sValue);
                    break;
                case 2:
                    ShowInformationByGraph(interfaceName, rValue, sValue);
                    break;
                default:
                    break;
            }

        }

        private void ShowInformationByGraph(string interfaceName, float rValue, float sValue)
        {
            //throw new NotImplementedException();
        }
        float maxRValue = 0, maxSValue = 0;
        private void ShowInformationByBar(string interfaceName, float rValue, float sValue)
        {
            System.Drawing.Font f = new System.Drawing.Font("Batang", 8);
            string textR = "R {0} Ko";
            string textS = "S {0} Ko";

            screen.ClearBackground();
            screen.BeginDrawing();
            screen.GraphicDevice.DrawString(string.Format("{0}",interfaceName) , f, System.Drawing.Brushes.Black, 0, 0);

            screen.GraphicDevice.DrawString(string.Format(textR, (rValue/1000).ToString("0.00")), f, System.Drawing.Brushes.Black, 0, 30);
            screen.GraphicDevice.DrawString(string.Format(textS, (sValue/1000).ToString("0.00")), f, System.Drawing.Brushes.Black, 80, 30);

            maxRValue = rValue > maxRValue ? rValue : maxRValue;
            maxSValue = sValue > maxSValue ? sValue : maxSValue;

            var rSize = 30 * rValue / maxRValue;
            var sSize = 30 * sValue / maxSValue;
            screen.GraphicDevice.DrawRectangle(System.Drawing.Pens.Black, 10, screen.ScreenHeight - 1 - rSize, 60, rSize);
            screen.GraphicDevice.DrawRectangle(System.Drawing.Pens.Black, 90, screen.ScreenHeight - 1 - sSize, 60, sSize);

            screen.EndDrawing();
        }

        private void ShowInformationByText(string interfaceName, float rValue, float sValue)
        {
            string textR = string.Format("Bytes received : {0} Ko", (rValue/1000).ToString("0.00"));
            string textS = string.Format("Bytes sent : {0} Ko", (sValue/1000).ToString("0.00"));

            System.Drawing.Font f = new System.Drawing.Font("Batang", 8);

            screen.ClearBackground();
            screen.BeginDrawing();
            screen.GraphicDevice.DrawString(string.Format("{0}", interfaceName), f, System.Drawing.Brushes.Black, 0, 0);
            screen.GraphicDevice.DrawString(textR, f, System.Drawing.Brushes.Black, 0, 12);
            screen.GraphicDevice.DrawString(textS, f, System.Drawing.Brushes.Black, 0, 24);
            screen.EndDrawing();

        }

        void screen_ButtonClicked(object sender, LCDButtonsClickedEventArgs e)
        {
            switch (e.Button)
            {
                case LCDButtons.MonoButton1:
                    counterIdx = (counterIdx + 1) % bytesReceived.Count;
                    break;
                case LCDButtons.MonoButton2:
                    counterIdx = counterIdx - 1 < 0 ? bytesReceived.Count - 1 : counterIdx - 1;
                    break;
                case LCDButtons.MonoButton3:
                    displayTypeIdx = (displayTypeIdx + 1) % 3;
                    break;
                case LCDButtons.MonoButton4:
                    displayTypeIdx = displayTypeIdx - 1 < 0 ? 2 : displayTypeIdx - 1;
                    break;
                case LCDButtons.None:
                    break;
            }
        }


        private void btnGetNetworkTraffic_Click(object sender, RoutedEventArgs e)
        {
            //cbxCounterCategories.DataContext = System.Diagnostics.PerformanceCounterCategory.GetCategories().OrderBy( cat => cat.CategoryName) ;

            System.Diagnostics.PerformanceCounterCategory cat = new System.Diagnostics.PerformanceCounterCategory("Network Interface", ".");
            bytesReceived = new List<System.Diagnostics.PerformanceCounter>();
            bytesSent = new List<System.Diagnostics.PerformanceCounter>();

            foreach (var item in cat.GetInstanceNames())
            {
                bytesReceived.Add(new System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Received/sec", item));
                bytesSent.Add(new System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Sent/sec", item));
            }
            refreshTimer.Start();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            refreshTimer.Stop();
            screen.Dispose();
        }


    }
}
